package io.renren.modules.sys.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.common.utils.Tools;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.DepotEntity;
import io.renren.modules.sys.entity.DepotitemEntity;
import io.renren.modules.sys.entity.MaterialEntity;
import io.renren.modules.sys.entity.UnitEntity;
import io.renren.modules.sys.service.DepotService;
import io.renren.modules.sys.service.DepotitemService;
import io.renren.modules.sys.service.MaterialService;
import io.renren.modules.sys.service.UnitService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * 单据子表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-11-22 14:29:36
 */
@RestController
@RequestMapping("sys/depotitem")
public class DepotitemController {
    @Autowired
    private DepotitemService depotitemService;
    @Autowired
    private MaterialService materialService;
    @Autowired
    private UnitService unitService;
    @Autowired
    private DepotService depotService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:depotitem:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = depotitemService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/findBy")
    public R findBy(@RequestParam Map<String, Object> params){

        String pageSize = "15";
        String pageNo = "1";
        Object pageSizeObj = params.get("pageSize");
        Object pageNoObj = params.get("pageNo");
        if(pageSizeObj!=null){
            pageSize=pageSizeObj.toString();
        }
        if(pageNoObj!=null){
            pageNo=pageNoObj.toString();
        }
        params.put("limit",pageSize);
        params.put("page",pageNo);
        Page<DepotitemEntity> page = depotitemService.selectPage(
                new Query<DepotitemEntity>(params).getPage(),
                new EntityWrapper<DepotitemEntity>()
                        .like("HeaderId", params.get("HeaderId").toString())
                        .orderBy("Id")
        );

        PageUtils pageUtils = new PageUtils(page);
        List<DepotitemEntity> depotitemEntityList = (List<DepotitemEntity>) pageUtils.getList();
        //存放数据json数组
        JSONArray dataArray = new JSONArray();

        if (null != depotitemEntityList) {
            for (DepotitemEntity depotItem : depotitemEntityList) {
                DepotEntity depot = depotService.selectById(depotItem.getDepotid());
                DepotEntity anotherdepot = depotService.selectById(depotItem.getAnotherdepotid());
                Long materialid = depotItem.getMaterialid();
                MaterialEntity materialEntity = materialService.selectById(materialid);
                UnitEntity unitEntity = unitService.selectById(materialEntity.getUnitid());
                JSONObject item = new JSONObject();
                item.put("Id", depotItem.getId());
                item.put("MaterialId", materialEntity == null ? "" :materialEntity.getId());
                String ratio; //比例
                if (unitEntity == null ) {
                    ratio = "";
                } else {
                    ratio = unitEntity.getUname();
                    ratio = ratio.substring(ratio.indexOf("("));
                }
                String[] mpArr =null;
                try {
                    String mpList = params.get("mpList").toString();
                    mpArr = mpList.split(",");
                }catch (Exception e){
                    mpArr =new String[]{"颜色","规格"};
                }
                //品名/型号/扩展信息/包装
                String MaterialName = materialEntity.getName() + ((materialEntity.getModel() == null || materialEntity.getModel().equals("")) ? "" : "(" + materialEntity.getModel() + ")");
                String materialOther = getOtherInfo(mpArr, materialEntity);
                MaterialName = MaterialName + materialOther + ((materialEntity.getUnit() == null || materialEntity.getUnit().equals("")) ? "" : "(" + materialEntity.getUnit() + ")") + ratio;
                item.put("MaterialName", MaterialName);
                item.put("Unit", depotItem.getMunit());
                item.put("OperNumber", depotItem.getOpernumber());
                item.put("BasicNumber", depotItem.getBasicnumber());
                item.put("UnitPrice", depotItem.getUnitprice());
                item.put("TaxUnitPrice", depotItem.getTaxunitprice());
                item.put("AllPrice", depotItem.getAllprice());
                item.put("Remark", depotItem.getRemark());
                item.put("Img", depotItem.getImg());
                item.put("DepotId", depot.getId() == null ? "" :depot.getId());
                item.put("DepotName", depot.getName() == null ? "" : depot.getName());
                item.put("AnotherDepotId", depotItem.getAnotherdepotid() == null ? "" : anotherdepot.getId());
                item.put("AnotherDepotName", depotItem.getAnotherdepotid() == null ? "" : anotherdepot.getName());
                item.put("TaxRate", depotItem.getTaxrate());
                item.put("TaxMoney", depotItem.getTaxmoney());
                item.put("TaxLastMoney", depotItem.getTaxlastmoney());
                item.put("OtherField1", depotItem.getOtherfield1());
                item.put("OtherField2", depotItem.getOtherfield2());
                item.put("OtherField3", depotItem.getOtherfield3());
                item.put("OtherField4", depotItem.getOtherfield4());
                item.put("OtherField5", depotItem.getOtherfield5());
                item.put("MType", depotItem.getMtype());
                item.put("op", 1);
                dataArray.add(item);
            }
        }

        return R.ok().put("total",depotitemEntityList.size()).put("rows",dataArray);
    }
    @RequestMapping("/findByAll")
    public R findByAll(@RequestParam Map<String, Object> params,String[] HeadIds,String[] MaterialIds){

        String pageSize = "15";
        String pageNo = "1";
        Object pageSizeObj = params.get("pageSize");
        Object pageNoObj = params.get("pageNo");
        if(pageSizeObj!=null){
            pageSize=pageSizeObj.toString();
        }
        if(pageNoObj!=null){
            pageNo=pageNoObj.toString();
        }
        params.put("limit",pageSize);
        params.put("page",pageNo);
        Page<DepotitemEntity> page = depotitemService.selectPage(
                new Query<DepotitemEntity>(params).getPage(),
                new EntityWrapper<DepotitemEntity>()
                        .in("HeaderId", HeadIds)
                        .in("MaterialId",MaterialIds)
                        .groupBy("MaterialId")
        );

        PageUtils pageUtils = new PageUtils(page);
        List<DepotitemEntity> depotitemEntityList = (List<DepotitemEntity>) pageUtils.getList();
        //存放数据json数组
        JSONArray dataArray = new JSONArray();

        String monthTime = params.get("MonthTime").toString();
        String pidStr = params.get("ProjectId").toString();
        Integer pid = Integer.valueOf(pidStr);
        String mpList = params.get("mpList").toString();
        String[] mpArr = mpList.split(",");
        if (null != depotitemEntityList) {
            for (DepotitemEntity depotItem : depotitemEntityList) {
                Long materialid = depotItem.getMaterialid();
                MaterialEntity materialEntity = materialService.selectById(materialid);
                JSONObject item = new JSONObject();
                Integer prevSum = sumNumber("入库", pid, materialEntity.getId(),monthTime, true) - sumNumber("出库", pid, materialEntity.getId(),monthTime, true);
                Integer InSum = sumNumber("入库", pid, materialEntity.getId(),monthTime, false);
                Integer OutSum = sumNumber("出库", pid, materialEntity.getId(),monthTime, false);
                Double prevPrice = sumPrice("入库", pid, materialEntity.getId(),monthTime, true) - sumPrice("出库", pid, materialEntity.getId(),monthTime, true);
                Double InPrice = sumPrice("入库", pid, materialEntity.getId(),monthTime, false);
                Double OutPrice = sumPrice("出库", pid, materialEntity.getId(),monthTime, false);
                item.put("Id", depotItem.getId());
                item.put("MaterialId", materialEntity == null ? "" : materialEntity.getId());
                item.put("MaterialName", materialEntity.getName());
                item.put("MaterialModel", materialEntity.getModel());
                //扩展信息
                String materialOther = getOtherInfo(mpArr, materialEntity);
                item.put("MaterialOther", materialOther);
                item.put("MaterialColor", materialEntity.getColor());
                item.put("MaterialUnit", materialEntity.getUnit());
                Double unitPrice = 0.0;
                if (prevSum + InSum - OutSum != 0.0) {
                    unitPrice = (prevPrice + InPrice - OutPrice) / (prevSum + InSum - OutSum);
                }
                item.put("UnitPrice", unitPrice);
                item.put("prevSum", prevSum);
                item.put("InSum", InSum);
                item.put("OutSum", OutSum);
                item.put("thisSum", prevSum + InSum - OutSum);
                item.put("thisAllPrice", prevPrice + InPrice - OutPrice);
                dataArray.add(item);
            }
        }

        return R.ok().put("total",depotitemEntityList.size()).put("rows",dataArray);
    }


    @RequestMapping("/buyIn")
    public R buyIn(@RequestParam Map<String, Object> params,String[] HeadIds,String[] MaterialIds){

        String pageSize = "15";
        String pageNo = "1";
        Object pageSizeObj = params.get("pageSize");
        Object pageNoObj = params.get("pageNo");
        if(pageSizeObj!=null){
            pageSize=pageSizeObj.toString();
        }
        if(pageNoObj!=null){
            pageNo=pageNoObj.toString();
        }
        params.put("limit",pageSize);
        params.put("page",pageNo);
        Page<DepotitemEntity> page = depotitemService.selectPage(
                new Query<DepotitemEntity>(params).getPage(),
                new EntityWrapper<DepotitemEntity>()
                        .in("HeaderId", HeadIds)
                        .in("MaterialId",MaterialIds)
                        .groupBy("MaterialId")
        );

        PageUtils pageUtils = new PageUtils(page);
        List<DepotitemEntity> depotitemEntityList = (List<DepotitemEntity>) pageUtils.getList();

        String monthTime = params.get("MonthTime").toString();
        String mpList = params.get("mpList").toString();
        String[] mpArr = mpList.split(",");
        Double thisAllPrice = 0.0;

        //存放数据json数组
        JSONArray dataArray = new JSONArray();
        if (null != depotitemEntityList) {
            for (DepotitemEntity depotItem : depotitemEntityList) {
                Long materialid = depotItem.getMaterialid();
                MaterialEntity materialEntity = materialService.selectById(materialid);
                JSONObject item = new JSONObject();
                Integer InSum = sumNumberBuyOrSale("入库", "采购", materialEntity.getId(), monthTime);
                Integer OutSum = sumNumberBuyOrSale("出库", "采购退货", materialEntity.getId(), monthTime);
                Double InSumPrice = sumPriceBuyOrSale("入库", "采购", materialEntity.getId(), monthTime);
                Double OutSumPrice = sumPriceBuyOrSale("出库", "采购退货", materialEntity.getId(), monthTime);
                item.put("Id", depotItem.getId());
                item.put("MaterialId", materialEntity == null ? "" : materialEntity.getId());
                item.put("MaterialName", materialEntity.getName());
                item.put("MaterialModel", materialEntity.getModel());
                //扩展信息
                String materialOther = getOtherInfo(mpArr, materialEntity);
                item.put("MaterialOther", materialOther);
                item.put("MaterialColor", materialEntity.getColor());
                item.put("MaterialUnit", materialEntity.getUnit());
                item.put("InSum", InSum);
                item.put("OutSum", OutSum);
                item.put("InSumPrice", InSumPrice);
                item.put("OutSumPrice", OutSumPrice);
                dataArray.add(item);
            }
        }

        return R.ok().put("total",depotitemEntityList.size()).put("rows",dataArray);
    }
    @RequestMapping("/saleOut")
    public R saleOut(@RequestParam Map<String, Object> params,String[] HeadIds,String[] MaterialIds){

        String pageSize = "15";
        String pageNo = "1";
        Object pageSizeObj = params.get("pageSize");
        Object pageNoObj = params.get("pageNo");
        if(pageSizeObj!=null){
            pageSize=pageSizeObj.toString();
        }
        if(pageNoObj!=null){
            pageNo=pageNoObj.toString();
        }
        params.put("limit",pageSize);
        params.put("page",pageNo);
        Page<DepotitemEntity> page = depotitemService.selectPage(
                new Query<DepotitemEntity>(params).getPage(),
                new EntityWrapper<DepotitemEntity>()
                        .in("HeaderId", HeadIds)
                        .in("MaterialId",MaterialIds)
                        .groupBy("MaterialId")
        );

        PageUtils pageUtils = new PageUtils(page);
        List<DepotitemEntity> depotitemEntityList = (List<DepotitemEntity>) pageUtils.getList();

        String monthTime = params.get("MonthTime").toString();
        String mpList = params.get("mpList").toString();
        String[] mpArr = mpList.split(",");
        Double thisAllPrice = 0.0;

        //存放数据json数组
        JSONArray dataArray = new JSONArray();
        if (null != depotitemEntityList) {
            for (DepotitemEntity depotItem : depotitemEntityList) {
                Long materialid = depotItem.getMaterialid();
                MaterialEntity materialEntity = materialService.selectById(materialid);
                JSONObject item = new JSONObject();
                Integer OutSumRetail = sumNumberBuyOrSale("出库", "零售", materialEntity.getId(), monthTime);
                Integer OutSum = sumNumberBuyOrSale("出库", "销售", materialEntity.getId(), monthTime);
                Integer InSumRetail = sumNumberBuyOrSale("入库", "零售退货", materialEntity.getId(), monthTime);
                Integer InSum = sumNumberBuyOrSale("入库", "销售退货", materialEntity.getId(), monthTime);
                Double OutSumRetailPrice = sumPriceBuyOrSale("出库", "零售", materialEntity.getId(), monthTime);
                Double OutSumPrice = sumPriceBuyOrSale("出库", "销售", materialEntity.getId(), monthTime);
                Double InSumRetailPrice = sumPriceBuyOrSale("入库", "零售退货",materialEntity.getId(), monthTime);
                Double InSumPrice = sumPriceBuyOrSale("入库", "销售退货", materialEntity.getId(), monthTime);
                item.put("Id", depotItem.getId());
                item.put("MaterialId", materialEntity == null ? "" : materialEntity.getId());
                item.put("MaterialName", materialEntity.getName());
                item.put("MaterialModel", materialEntity.getModel());
                //扩展信息
                String materialOther = getOtherInfo(mpArr, materialEntity);
                item.put("MaterialOther", materialOther);
                item.put("MaterialColor", materialEntity.getColor());
                item.put("MaterialUnit",materialEntity.getUnit());
                item.put("OutSum", OutSumRetail + OutSum);
                item.put("InSum", InSumRetail + InSum);
                item.put("OutSumPrice", OutSumRetailPrice + OutSumPrice);
                item.put("InSumPrice", InSumRetailPrice + InSumPrice);
                dataArray.add(item);
            }
        }

        return R.ok().put("total",depotitemEntityList.size()).put("rows",dataArray);
    }

    @RequestMapping("/findGiftByAll")
    public R findGiftByAll(@RequestParam Map<String, Object> params,String[] HeadIds,String[] MaterialIds){

        String pageSize = "15";
        String pageNo = "1";
        Object pageSizeObj = params.get("pageSize");
        Object pageNoObj = params.get("pageNo");
        if(pageSizeObj!=null){
            pageSize=pageSizeObj.toString();
        }
        if(pageNoObj!=null){
            pageNo=pageNoObj.toString();
        }
        params.put("limit",pageSize);
        params.put("page",pageNo);
        Page<DepotitemEntity> page = depotitemService.selectPage(
                new Query<DepotitemEntity>(params).getPage(),
                new EntityWrapper<DepotitemEntity>()
                        .in("HeaderId", HeadIds)
                        .in("MaterialId",MaterialIds)
                        .groupBy("MaterialId")
        );

        PageUtils pageUtils = new PageUtils(page);
        List<DepotitemEntity> depotitemEntityList = (List<DepotitemEntity>) pageUtils.getList();
        String pidStr = params.get("ProjectId").toString();
        int pid = Integer.valueOf(pidStr);
        String[] mpArr =null;
        try {
            String mpList = params.get("mpList").toString();
            mpArr = mpList.split(",");
        }catch (Exception e){
            mpArr =new String[]{"颜色","规格"};
        }

        //存放数据json数组
        JSONArray dataArray = new JSONArray();
        if (null != depotitemEntityList) {
            for (DepotitemEntity depotItem : depotitemEntityList) {
                Long materialid = depotItem.getMaterialid();
                MaterialEntity materialEntity = materialService.selectById(materialid);
                JSONObject item = new JSONObject();
                Integer InSum = sumNumberGift("礼品充值", pid, materialEntity.getId(), "in");
                Integer OutSum = sumNumberGift("礼品销售", pid, materialEntity.getId(), "out");
                item.put("Id", depotItem.getId());
                item.put("MaterialId",materialEntity== null ? "" : materialEntity.getId());
                item.put("MaterialName", materialEntity.getName());
                item.put("MaterialModel", materialEntity.getModel());
                //扩展信息
                String materialOther = getOtherInfo(mpArr, materialEntity);
                item.put("MaterialOther", materialOther);
                item.put("MaterialColor", materialEntity.getColor());
                item.put("MaterialUnit", materialEntity.getUnit());
                item.put("thisSum", InSum - OutSum);
                dataArray.add(item);
            }
        }

        return R.ok().put("total",depotitemEntityList.size()).put("rows",dataArray);
    }


    @RequestMapping("/totalCountMoney")
    public R totalCountMoney(@RequestParam Map<String, Object> params,String[] HeadIds,String[] MaterialIds){

        String pageSize = "15";
        String pageNo = "1";
        Object pageSizeObj = params.get("pageSize");
        Object pageNoObj = params.get("pageNo");
        if(pageSizeObj!=null){
            pageSize=pageSizeObj.toString();
        }
        if(pageNoObj!=null){
            pageNo=pageNoObj.toString();
        }
        params.put("limit",pageSize);
        params.put("page",pageNo);
        Page<DepotitemEntity> page = depotitemService.selectPage(
                new Query<DepotitemEntity>(params).getPage(),
                new EntityWrapper<DepotitemEntity>()
                        .in("HeaderId", HeadIds)
                        .in("MaterialId",MaterialIds)
                        .groupBy("MaterialId")
        );

        PageUtils pageUtils = new PageUtils(page);
        List<DepotitemEntity> depotitemEntityList = (List<DepotitemEntity>) pageUtils.getList();

        String monthTime = params.get("MonthTime").toString();
        String pidStr = params.get("ProjectId").toString();
        Integer pid = Integer.valueOf(pidStr);
        Double thisAllPrice = 0.0;
        if (null != depotitemEntityList) {
            for (DepotitemEntity depotItem : depotitemEntityList) {
                Long materialid = depotItem.getMaterialid();
                MaterialEntity materialEntity = materialService.selectById(materialid);
                Double prevPrice = sumPrice("入库", pid, materialEntity.getId(), monthTime, true) - sumPrice("出库", pid, materialEntity.getId(), monthTime, true);
                Double InPrice = sumPrice("入库", pid, materialEntity.getId(), monthTime, false);
                Double OutPrice = sumPrice("出库", pid, materialEntity.getId(), monthTime, false);
                thisAllPrice = thisAllPrice + (prevPrice + InPrice - OutPrice);
            }
        }

        return R.ok().put("totalCount",thisAllPrice);
    }
    @RequestMapping("/findStockNumByMaterialId")
    public R findStockNumByMaterialId(@RequestParam Map<String, Object> params){

        Integer pageSize = 15;
        Integer pageNo = 1;
        Object pageSizeObj = params.get("pageSize");
        Object pageNoObj = params.get("pageNo");
        if(pageSizeObj!=null){
            pageSize=Integer.parseInt(pageSizeObj.toString());
        }
        if(pageNoObj!=null){
            pageNo=Integer.parseInt(pageNoObj.toString());
        }

        int start = pageSize*(pageNo-1);
        int end = pageSize*pageNo-1;
        params.put("start",start);
        params.put("end",end);
        String mid = Tools.dealNullStr(params.get("MaterialId"));
        List<HashMap> depotitemEntityList=   depotitemService.findDetailByTypeAndMaterialId(mid,start,end);
        //存放数据json数组
        JSONArray dataArray = new JSONArray();
        if (depotitemEntityList != null) {
            for (Integer i = 0; i < depotitemEntityList.size(); i++) {
                JSONObject item = new JSONObject();
                HashMap map = depotitemEntityList.get(i); //获取对象
                item.put("Number", map.get("Number")); //商品编号
                item.put("Type", map.get("newType")); //进出类型
                item.put("BasicNumber", map.get("b_num")); //数量
                item.put("OperTime", map.get("oTime")); //时间
                dataArray.add(item);
            }
        }

        return R.ok().put("total",depotitemEntityList.size()).put("rows",dataArray);
    }
    @RequestMapping("/findDetailByTypeAndMaterialId")
    public R findDetailByTypeAndMaterialId(@RequestParam Map<String, Object> params){

        String pageSize = "15";
        String pageNo = "1";
        Object pageSizeObj = params.get("pageSize");
        Object pageNoObj = params.get("pageNo");
        if(pageSizeObj!=null){
            pageSize=pageSizeObj.toString();
        }
        if(pageNoObj!=null){
            pageNo=pageNoObj.toString();
        }
        params.put("limit",pageSize);
        params.put("page",pageNo);
        Page<DepotitemEntity> page = depotitemService.selectPage(
                new Query<DepotitemEntity>(params).getPage(),
                new EntityWrapper<DepotitemEntity>()
                        .like("MaterialId", Tools.dealNullStr(params.get("MaterialId")))
        );

        PageUtils pageUtils = new PageUtils(page);
        List<DepotitemEntity> depotitemEntityList = (List<DepotitemEntity>) pageUtils.getList();
        //存放数据json数组
        JSONArray dataArray = new JSONArray();
        if (null != depotitemEntityList) {
            for (DepotitemEntity depotItem : depotitemEntityList) {
                Long materialid = depotItem.getMaterialid();
                MaterialEntity materialEntity = materialService.selectById(materialid);
                JSONObject item = new JSONObject();
                Integer InSum = sumNumberByMaterialId("入库", materialEntity.getId());
                Integer OutSum = sumNumberByMaterialId("出库", materialEntity.getId());
                item.put("MaterialId",materialEntity== null ? "" : materialEntity.getId());
                item.put("MaterialName", materialEntity.getName());
                item.put("MaterialModel", materialEntity.getModel());
                item.put("thisSum", InSum - OutSum);
                dataArray.add(item);
            }
        }

        return R.ok().put("total",depotitemEntityList.size()).put("rows",dataArray);
    }


    public Integer sumNumberByMaterialId(String type, Long MId) {
        Integer sumNumber = 0;
        try {
            sumNumber= depotitemService.findByTypeAndMaterialId(type, MId);
            if (sumNumber==null) {
                sumNumber = 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sumNumber;
    }



    public Integer sumNumberGift(String subType, Integer ProjectId, Long MId, String type) {
        Integer sumNumber = 0;
        try {
            sumNumber=  depotitemService.findGiftByType( subType, ProjectId, MId, type);
            if (sumNumber==null) {
                sumNumber = 0;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return sumNumber;
    }


    public Integer sumNumber(String type, Integer ProjectId, Long MId, String MonthTime, Boolean isPrev) {
        Integer sumNumber=0;
        try {
            sumNumber=   depotitemService.findByType(type, ProjectId, MId, MonthTime, isPrev);
            if(sumNumber==null){
                return 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sumNumber;
    }


    public Integer sumNumberBuyOrSale(String type, String subType, Long MId, String MonthTime) {
        Double sum = 0.00;
        String sumType = "Number";
        try {
            sum = depotitemService.buyOrSale(type, subType, MId, MonthTime, sumType);
            if (sum==null) {
                sum = 0.00;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (int)Math.round(sum);
    }

    @SuppressWarnings("unchecked")
    public Double sumPriceBuyOrSale(String type, String subType, Long MId, String MonthTime) {
        Double sumPrice = 0.00;
        String sumType = "Price";
        try {
            sumPrice = depotitemService.buyOrSale(type, subType, MId, MonthTime, sumType);
            if (sumPrice==null) {
                sumPrice =0.00;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sumPrice;
    }



    /**
     * 价格合计
     *
     * @param type
     * @param MId
     * @param MonthTime
     * @param isPrev
     * @return
     */
    @SuppressWarnings("unchecked")
    public Double sumPrice(String type, Integer ProjectId, Long MId, String MonthTime, Boolean isPrev) {
        Double sumPrice = 0.00;
        try {
            sumPrice=   depotitemService.findPriceByType(type, ProjectId, MId, MonthTime, isPrev);
            if(sumPrice==null){
                return 0.00;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sumPrice;
    }





    /**
     * 获取扩展信息
     *
     * @return
     */
    public String getOtherInfo(String[] mpArr, MaterialEntity materialEntity) {
        String materialOther = "";
        for (int i = 0; i < mpArr.length; i++) {
            if (mpArr[i].equals("颜色")) {
                materialOther = materialOther + ((materialEntity.getColor() == null || materialEntity.getColor().equals("")) ? "" : "(" + materialEntity.getColor() + ")");
            }
            if (mpArr[i].equals("规格")) {
                materialOther = materialOther + ((materialEntity.getStandard() == null || materialEntity.getStandard().equals("")) ? "" : "(" + materialEntity.getStandard() + ")");
            }
            if (mpArr[i].equals("制造商")) {
                materialOther = materialOther + ((materialEntity.getMfrs() == null || materialEntity.getMfrs().equals("")) ? "" : "(" + materialEntity.getMfrs() + ")");
            }
            if (mpArr[i].equals("自定义1")) {
                materialOther = materialOther + ((materialEntity.getOtherfield1() == null || materialEntity.getOtherfield1().equals("")) ? "" : "(" + materialEntity.getOtherfield1() + ")");
            }
            if (mpArr[i].equals("自定义2")) {
                materialOther = materialOther + ((materialEntity.getOtherfield2() == null || materialEntity.getOtherfield2().equals("")) ? "" : "(" + materialEntity.getOtherfield2() + ")");
            }
            if (mpArr[i].equals("自定义3")) {
                materialOther = materialOther + ((materialEntity.getOtherfield3() == null || materialEntity.getOtherfield3().equals("")) ? "" : "(" + materialEntity.getOtherfield3() + ")");
            }
        }
        return materialOther;
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:depotitem:info")
    public R info(@PathVariable("id") Long id){
        DepotitemEntity depotitem = depotitemService.selectById(id);

        return R.ok().put("depotitem", depotitem);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:depotitem:save")
    public R save(@RequestBody DepotitemEntity depotitem){
        depotitemService.insert(depotitem);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:depotitem:update")
    public R update(@RequestBody DepotitemEntity depotitem){
        ValidatorUtils.validateEntity(depotitem);
        depotitemService.updateAllColumnById(depotitem);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:depotitem:delete")
    public R delete(@RequestBody Long[] ids){
        depotitemService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
